code du robot
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import Port, Stop, Direction, Button, Color, SoundFile
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile
# This program requires LEGO EV3 MicroPython v2.0 or higher.
# Click "Open user guide" on the EV3 extension tab for more information.
# Create your objects here.
ev3 = EV3Brick()
moteur_gauche = Motor(Port.A)
moteur_potentiometre = Motor(Port.D)
bras_pousser1 = Motor(Port.B)
bras_pousser2 = Motor(Port.C)
Capteur = ColorSensor(Port.S4)
telecommande = InfraredSensor(Port.S2)
bouton_uregence = TouchSensor(Port.S3)
# variables tapis roulant
angle_old = 0
vitesse = 0
# variables bras
V = 270
#variable liste
rouge = 0
bleu = 0
vert = 0
jaune = 0
#variable angle bras
angleD = 110
angle1 = 170
angle2 = 250
#variable led brick
lumiere = 0
# intialisations des bras
ev3.speaker.play_file(SoundFile.HELLO)bras_pousser1.run(-90)bras_pousser2.run(-90)
wait(2000)
bras_pousser1.stop()
bras_pousser2.stop()
bras_pousser1.reset_angle(0)
bras_pousser2.reset_angle(0)
bras_pousser1.run_target(V, 90, wait=False)
bras_pousser2.run_target(V, 90, wait=False)
wait(1000)
ev3.screen.clear()
while True :
#tapis roulant potentiometre
angle = moteur_potentiometre.angle()
if angle > angle_old :
vitesse = vitesse +15
if vitesse > 360 :
vitesse = 360
elif angle < angle_old :
vitesse = vitesse -15
if vitesse < 0 :
vitesse = 0
moteur_gauche.run(-vitesse)
angle_old = angle
#bouton arret d urgence
if bouton_uregence.pressed():
vitesse = 0
# telecommande
liste_boutons = telecommande.buttons(1)
if liste_boutons != []:
print(liste_boutons)
elt_liste = liste_boutons.pop()
if elt_liste == Button.LEFT_UP:
vitesse = vitesse +15
if vitesse > 360 :
vitesse = 360
elif elt_liste == Button.LEFT_DOWN:
vitesse = vitesse -15
if vitesse < 0 :
vitesse = 0
elif elt_liste == Button.BEACON:
vitesse = 0
#capteur , bras
couleur = Capteur.color()
t = Capteur.rgb()
r = t[0]
g = t[1]
b = t[2]
#rouge
if r > 25 and g < 25 and b < 25 :
bras_pousser1.run_target(V, angle1, wait=False)
bras_pousser2.run_target(V, angleD, wait=False)
ev3.speaker.play_file('rouge.wav')
ev3.light.on(Color.RED)
rouge = rouge + 1
lumiere = 0
#jaune
elif r > 40 and g > 40 and b < 40 :
bras_pousser2.run_target(V, angle1, wait=False)
bras_pousser1.run_target(V, angleD, wait=False)
ev3.speaker.play_file(SoundFile.YELLOW)
ev3.light.on(Color.YELLOW)
jaune = jaune + 1
lumiere = 0
#vert
elif r < 25 and g > 25 and b < 25 :
bras_pousser2.run_target(V, angle2, wait=False)
bras_pousser1.run_target(V, angleD, wait=False)
ev3.speaker.play_file(SoundFile.GREEN)
ev3.light.on(Color.GREEN)
vert = vert + 1
lumiere = 0
#bleu
elif r < 25 and g < 40 and b > 20 :
bras_pousser2.run_target(V, angleD, wait=False)
bras_pousser1.run_target(V, angle2, wait=False)
ev3.speaker.play_file(SoundFile.BLUE)
ev3.light.on(Color.BLUE)
bleu = bleu + 1
lumiere = 0
# liste couleur
ev3.screen.clear()
ev3.screen.draw_text(5, 5, "Rouge " + str(rouge))
ev3.screen.draw_text(5, 20, "Vert " + str(vert))
ev3.screen.draw_text(5, 35, "Bleu " + str(bleu))
ev3.screen.draw_text(5, 50, "Jaune " + str(jaune))
lumiere = lumiere + 1
if lumiere > 5:
ev3.light.off()
wait(200)